package com.ls.fw.data.bean;

import java.util.ArrayList;
import java.util.List;

import com.ls.fw.data.visitor.FieldVisitor;


/**
 * 记录实体
 * @author ls
 * 2014年11月21日下午6:24:08
 */
public class Record implements Cloneable{
	
	private String schemaName = "";
	private String table = "";
	private long version = 0;
	private float score = 0f;

	private List<Column> columns = new ArrayList<Column>();

	
	public Record addField(String name,Object value){
		this.columns.add(new Column(name, value));
		return this;
	}
	
	public Record addField(Column column){
		this.columns.add(column);
		return this;
	}
	
	public List<Column> getFields() {
		return columns;
	}

	public void setFields(List<Column> columns) {
		this.columns = columns;
	}
	
	/**
	 * 获取本条记录的字段
	 * @author ls
	 * 2014年11月21日下午5:28:04 
	 * @param name
	 * @return
	 */
	public Column getField(String name){
		for (Column column : columns) {
			if(column.getShowName().equals(name) || column.getName().equals(name)){
				return column;
			}
		}
		return null;
	}
	
	
	public void clear(){
		this.columns.clear();
	}

	
	public long getVersion() {
		return version;
	}


	public Record setVersion(long version) {
		this.version = version;
		return this;
	}


	public float getScore() {
		return score;
	}


	public Record setScore(float score) {
		this.score = score;
		return this;
	}

	@Override
	public String toString() {
		return "Record [schemaName=" + schemaName + ", table=" + table
				+ ", version=" + version + ", score=" + score + ", fields="
				+ columns + "]";
	}

	
	/**
	 * 浏览该记录字段
	 * @author ls
	 * 2014年12月1日上午11:26:49 
	 * @param fieldVisitor
	 */
	public int accept(FieldVisitor fieldVisitor) {
		if(columns != null && !columns.isEmpty()){
			for (Column column : columns) {
				fieldVisitor.visit(column);
			}
			return columns.size();
		}
		return 0;
	}

	public String getSchemaName() {
		return schemaName;
	}

	public Record setSchemaName(String schemaName) {
		this.schemaName = schemaName;
		return this;
	}

	public String getTable() {
		return table;
	}

	public Record setTable(String table) {
		this.table = table;
		return this;
	}
	
	public boolean hasField(){
		return (columns!=null && !columns.isEmpty())?false:true;
	}
	
	public String printTable(){
		StringBuilder sb = new StringBuilder();
		List<Column> fs = this.columns;
		sb.append("序号");
		for (Column column : fs) {
			sb.append(" | ");
			sb.append(column.getShowName());
		}
		sb.append("\n");
		int index1 = 0;
		List<Column> fs1 = fs;
		sb.append(++index1);
		for (Column column : fs1) {
			sb.append(" | ");
			sb.append(column.getValue());
		}
		sb.append("\n");
		return sb.toString();
	}
}
